package servlet;

import entity.Rent;
import service.RentService;
import service.impl.RentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/RentServlet")
public class RentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        String opr = "list";
        RentService service = new RentServiceImpl();

        if (req.getParameter("opr") != null){
            opr = req.getParameter("opr");
        }

        if (opr.equals("list")){
            int pageIndex = 1;
            if (req.getParameter("pageIndex") != null){
                pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
            }
            int pageSize = 3;
            int pageCount = service.pageCount(pageSize);
            List<Rent> list = service.list(pageIndex,pageSize);
            req.setAttribute("list",list);
            req.setAttribute("pageIndex",pageIndex);
            req.setAttribute("pageCount",pageCount);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }else if (opr.equals("upd")){
            int id = Integer.parseInt(req.getParameter("id"));
            int status = Integer.parseInt(req.getParameter("status"));
            int r = service.update(id,status);
            if (r > 0){
                out.print("退租成功");
            }else
                out.print("退租失败");
        }else if (opr.equals("delete")){
            int id = Integer.parseInt(req.getParameter("id"));
            int r = service.delete(id);
            if (r > 0){
                out.print("删除成功");
            }else
                out.print("删除失败");
        }else if (opr.equals("add")){
            System.out.println("添加");
            String userName = req.getParameter("userName");
            String carNum = req.getParameter("carNum");
            int status = 0;
            String maturityDate = req.getParameter("maturityDate");
            Rent rent = new Rent();
            rent.setUserName(userName);
            rent.setCarNum(carNum);
            rent.setStatus(status);
            rent.setMaturityDate(maturityDate);
            int r = service.add(rent);

            if (r > 0){
                out.print("<script>alert('添加成功');location.href='RentServlet';</script>");
            }else
                out.print("<script>alert('添加失败');");
        }else if (opr.equals("byUserName")){
            int pageIndex = 1;
            String userName = req.getParameter("userName");
            if (req.getParameter("pageIndex") != null){
                pageIndex = Integer.parseInt(req.getParameter("pageIndex"));
            }
            int pageSize = 3;
            int pageCount = service.pageCount(pageSize);
            List<Rent> list = service.listByUserName(pageIndex,pageSize,userName);
            req.setAttribute("list",list);
            req.setAttribute("pageIndex",pageIndex);
            req.setAttribute("pageCount",pageCount);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
